package com.thinvent.nj.ais.service.impl;

import com.thinvent.nj.common.page.Page;
import com.thinvent.nj.common.page.QueryParameter;
import com.thinvent.nj.mybatis.service.impl.BaseCURDServiceImpl;
import com.thinvent.nj.ais.entity.Server;
import com.thinvent.nj.ais.mapper.ServerMapper;
import com.thinvent.nj.ais.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author liupj
* @date 2018-4-24
*/
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class ServerServiceImpl extends BaseCURDServiceImpl<Server, String> implements ServerService {

    @Autowired
    private ServerMapper serverMapper;

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public String getServerNameByUri(String uri) {
        return serverMapper.getServerNameByUri(uri);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public Map<String, String> getServerMapByUri(String uri) {
        List<Server> serverList = serverMapper.getServerListByUri(uri);
        Map<String, String> serverMap = new HashMap<>(5);

        for (Server item : serverList) {
            serverMap.put(item.getProjectNo(), item.getUri());
        }

        return serverMap;
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public Server get(String s) {
        return super.get(s);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Override
    public void delete(String s) {
        super.delete(s);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Override
    public void update(Server server) {
        super.update(server);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Override
    public void insert(Server server) {
        super.insert(server);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Server> findList(Map<String, Object> params) {
        return super.findList(params);
    }

    @Cacheable(cacheNames = "sys", key = "'ServerServiceImpl.findPage:' + #queryParameter.pageNo + '_' + #queryParameter.pageSize + '_' + #params")
    @Override
    public Page<Server> findPage(QueryParameter queryParameter, Map<String, Object> params) {
        return super.findPage(queryParameter, params);
    }
}
